const http = require('http')
const qs=require('querystring')
const config=require('../config') 
const apiaddress=require('./apiaddress')


const getDict = async (ctx,next) => {  //获取AG兑换比例
    let {coin}=ctx.params
    const result = new Promise((resolve,reject) => {
        let opt={
            hostname:config.hostname,
            port: config.port,
            path:`/dict/dict/ag-rate-${coin}`,
            method: "GET"
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata))
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.end()
    })
    let data=await result
    if(data.code){
        ctx.error(data)
    }else{
        ctx.success(data)
    }
}

const buyAgCredit= async (ctx,next) => { //将虚拟币兑换为AG分
    const userId =  ctx.session.userId
    let {coin,amount}=ctx.request.body
    let params={
        userId:userId,
        coin:coin,
        amount:amount
    }
    let postdata=qs.stringify(params)
    const result=new Promise((resolve,reject) => {
        let opt={
            hostname:config.aghostname,
            port: config.agport,
            path:`${apiaddress.transferToAg}`,
            method: "POST",
            headers:{
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': postdata.length
            }
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata))
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.write(postdata)
        req.end()
    })
    let data=await result
    if(data.code){
        ctx.error(data)
    }else{
        ctx.success(data)
    }
}

const getagBalance = async (ctx,next) => { //获取AG积分
    let {coin}=ctx.request.body
    const userId =  ctx.session.userId
    if(userId){
        const result = new Promise((resolve,reject) => {
            let opt={
                hostname:config.aghostname,
                port: config.agport,
                path:`${apiaddress.getAgBalance}?userId=${userId}&coin=${coin}`,
                method: "GET"
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.end()
        })
        let data=await result
        if(data.code){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }else{
        let data={
            code:200,
            data:null
        }
        ctx.success(data)
    }
}

const getGameUrl = async (ctx,next) => {  //获取AG游戏地址
    const userId =  ctx.session.userId
    let {coin,gameType,token,isMobile}=ctx.request.body
    let params={
        userId:userId,
        coin:coin,
        gameType:gameType,
        token:token,
        isMobile:isMobile
    }
    let getdata=qs.stringify(params)
    const result=new Promise((resolve,reject) => {
        let opt={
            hostname:config.aghostname,
            port: config.agport,
            path:`${apiaddress.getGameUrl}?${getdata}`,
            method: "GET"
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata))
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.end()
    })
    let data=await result
    if(data.code){
        ctx.error(data)
    }else{
        ctx.success(data)
    }
}

const transferFromAg = async (ctx,next) => { //从AG兑回币
    const userId =  ctx.session.userId
    let {coin,amount}=ctx.request.body
    let params={
        userId:userId,
        coin:coin,
        amount:amount
    }
    let postdata=qs.stringify(params)
    const result=new Promise((resolve,reject) => {
        let opt={
            hostname:config.aghostname,
            port: config.agport,
            path:`${apiaddress.transferFromAg}`,
            method: "POST",
            headers:{
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': postdata.length
            }
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata))
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.write(postdata)
        req.end()
    })
    let data=await result
    if(data.code){
        ctx.error(data)
    }else{
        ctx.success(data)
    }
}

module.exports={
    getDict,
    buyAgCredit,
    getagBalance,
    getGameUrl,
    transferFromAg
}